package web.PetStore.web.servlet;


import web.PetStore.domain.Account;
import web.PetStore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "RegisterServlet", urlPatterns = {"/Register"})
public class RegisterServlet extends HttpServlet {

    private String userid, password, repeatPassword, phone, email, address, registerInfo, checkcode, captcha;


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.userid = req.getParameter("userid");
        this.password = req.getParameter("password");
        this.repeatPassword = req.getParameter("repeatPassword");
        this.phone = req.getParameter("phone");
        this.email = req.getParameter("email");
        this.address = req.getParameter("address");
        this.checkcode = req.getParameter("checkcode");

        if(!this.validate()){
            req.setAttribute("registerInfo" ,this.registerInfo);
            req.getRequestDispatcher("RegisterForm").forward(req, resp);
        }else{
            Account registerUser = new Account();
            registerUser.setUserid(this.userid);
            registerUser.setPassword(this.password);
            registerUser.setPhone(this.phone);
            registerUser.setEmail(this.email);
            registerUser.setAddress(this.address);
            AccountService accountService = new AccountService();
            if(accountService.newUser(registerUser)){
                resp.sendRedirect("LoginForm");
            }else{
                req.setAttribute("registerInfo", accountService.getInfo());
                req.getRequestDispatcher("RegisterForm").forward(req,resp);
            }
        }

    }

    private boolean validate(){
        if(this.userid == null || this.userid.equals("")){
            this.registerInfo = "用户名不能为空";
            return false;
        }
        if(new AccountService().findUserById(this.userid)){
            this.registerInfo = "用户名已存在";
            return false;
        }
        if(this.password == null || this.password.equals("")){
            this.registerInfo = "密码不能为空";
            return false;
        }
        if(this.repeatPassword == null || this.repeatPassword.equals("")){
            this.registerInfo = "确认密码不能为空";
            return false;
        }
        if(!this.repeatPassword.equals(this.password)){
            this.registerInfo = "两次密码不一致";
            return false;
        }
        if(this.phone == null || this.phone.equals("")){
            this.registerInfo = "手机号码不能为空";
            return false;
        }
        if(!CaptchaServlet.checkCodeCorrect(checkcode)){
            this.registerInfo = "验证码错误";
            return false;
        }
        return true;
    }
}
